package com.day1.managersystem.service;

import com.day1.managersystem.dao.TeacherDao;
import com.day1.managersystem.domain.Teacher;

/**
 * @author 小坡
 * @date 2022/8/25  18:15
 **/
public class TeacherService {

    private TeacherDao teacherDao = new TeacherDao();

    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);
    }

    public boolean isExists(String id) {
//        1、获取库管对象中的数组
        Teacher[] teachers = teacherDao.findAllTeacher();
//        记录是否存在的作用
        boolean exists = false;
//        2、遍历数组，取出每一个元素，进行判断
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Teacher[] findAllTeacher() {
        Teacher[] allTeacher = teacherDao.findAllTeacher();
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher t = allTeacher[i];
            if (t != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allTeacher;
        } else {
            return null;
        }

    }

    public void delTeacher(String id) {
        teacherDao.delTeacher(id);
    }

    public void updateTeacher(String id, Teacher newTeacher) {
        teacherDao.updateTeacher(id,newTeacher);
    }
}
